package com.sunducation.waterflow.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

@Configuration
@MapperScan(
    basePackages = {"com.sunducation.waterflow.dao.mapper"},
    sqlSessionFactoryRef = "sqlSessionFactory1")
public class MybatisSchoolDbConfig {

  @Autowired
  @Qualifier("waterflowDataSource")
  private DataSource rbacDS;

  @Bean
  public SqlSessionFactory sqlSessionFactory1() throws Exception {
    SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
    factoryBean.setDataSource(rbacDS); // 使用titan数据源, 连接titan库
    factoryBean.setMapperLocations(
        new PathMatchingResourcePatternResolver()
            .getResources("classpath:mybatis/mapper/**/*.xml"));
    return factoryBean.getObject();
  }

  @Bean(name = "sqlSessionTemplate1")
  public SqlSessionTemplate sqlSessionTemplate1() throws Exception {
    // 批量插入
    SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory1()); // 使用上面配置的Factory
    //        SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactory1(),
    // ExecutorType.BATCH);
    return template;
  }

  @Bean(name = "transactionManager1")
  public DataSourceTransactionManager transactionManager1() {
    return new DataSourceTransactionManager(rbacDS);
  }
}
